function C = slslice(A, dim)
%SLSLICE divides an array into a cell array of slices
%
% [ Syntax ]
%   - C = slslice(A, dim)
%
% [ Arguments ]
%   - A:        The input array
%   - dim:      The dimension along which to slice
%   - C:        The cell array of slices
%
% [ Description ]
%   - C = slslice(A, dim) divides an array A into a cell array C of slices.
%     dim specifies the dimension along which the array is divided.
%     Suppose A is of size n1 x n2 x n3 x n4 x ...
%
%       - If dim is 0, then C is a cell array of size n1 x n2 x n3 x n4 x ...
%         with each slice being a scalar;
%       - If dim is 1, then C is a cell array of size n2 x n3 x n4 x ...,
%         with each slice being a column vector of length n1.
%       - If dim is 2, then C is a cell array of size n3 x n4 x ...,
%         with each slice being a 2D matrix of size n1 x n2.
%       - The rule can be applied to higher dimensions.
%
% [ History ]
%   - Created by Dahua Lin, on Dec 21, 2007
%   - Modified by Dahua Lin, on Dec 21, 2007
%       - based on c-mex implementation
%

%% parse and verify input arguments

error(nargchk(2, 2, nargin));

assert(isscalar(dim) && isnumeric(dim) && dim >= 0, ...
    'sltoolbox:slslice:invalidarg', ...
    'dim should be a nonnegative integer scalar.');

%% main

if isscalar(A)
    C = {A};
    
else
    dA = ndims(A);
    if dA == 2 && size(A,2) == 1
        dA = 1;
    end
    
    if dim < dA
        
        sizA = size(A);
        sizC = sizA(dim+1:end);
        if isscalar(sizC)
            sizC = [sizC, 1];
        end        
        
        % if ~issparse(A) && (isnumeric(A) && isreal(A) || islogical(A))
        if false %% temporially disable the c-mex implementation
            
            % use C-mex implemenation
            
            sizC = sizA(dim+1:end);
            if dim == 0
                sizslice = [1 1];
            elseif dim == 1
                sizslice = [sizA(1), 1];
            else
                sizslice = sizA(1:dim);
            end                
            
            C = arrayslice_cimp(A, int32(sizC), int32(sizslice));            
            
        else
        
            % use pure MATLAB implementation
            
            C = cell(sizC);
            nb = numel(C);

            if dim == 0
                for i = 1 : nb
                    C{i} = A(i);
                end

            elseif dim == 1
                for i = 1 : nb
                    C{i} = A(:,i);
                end

            elseif dim == 2
                for i = 1 : nb
                    C{i} = A(:,:,i);
                end

            else
                bn = prod(sizA(1:dim));
                ndx = reshape(1:bn, bsiz);
                for i = 0 : nb-1
                    C{i} = A(ndx + i*bn);
                end
            end
        
        end
    
    else
        C = {A};
    end
end








    
